$(function() {

	/* 判断输入框是否为空 */
	if (!String.prototype.trim) {
		String.prototype.trim = function() {
			return this.replace(/^\s+|\s+$/g, '');
		};
	}

	$("input").on("input", function(evt) {
		if ($('#username').val().trim().length != 0 && $('#password').val().trim().length != 0) {
			$("#loginBtn").removeAttr("disabled");
		} else {
			$("#loginBtn").prop("disabled", "disabled");
		}
	});

	// 失去焦点
	$("#password").blur(function() {
		if ($(this).val() == '') {
			$('.password-tip').show(200);
		} else {
			$('.password-tip').hide(200);
		}
	});
	$('#username').blur(function() {
		if ($(this).val() == '') {
			$('.username-tip').show(200);
		} else {
			$('.username-tip').hide(200);
		}
	});
	$('#confirmCode').blur(function() {
		if ($(this).val() == '') {
			$('.confirmCode-tip').show(200);
		} else {
			$('.confirmCode-tip').hide(200);
		}
	});

	function login() {
		$.ajax({
			type: "post",
			url: "./user/login.bto?account=" + $('#username').val() + "&passwd=" + $('#password').val(),
			dataType: "json",
			cache: false,
			success: function(data) {
				if (data.code == 0) {
					if (data.other.code == 0) {
						window.location.href = "main.html";
					} else if (data.other.code == 2) {
						$('.username-tip').html(data.other.message);
						$('.username-tip').show(200)
					} else {
						$('.password-tip').html(data.other.message);
						$('.password-tip').show(200);
					}

				}
			}
		});
	}

	$('#loginBtn').on('click', function() {
		login();
	});
	document.onkeydown = function(event) {
		var e = event || window.event || arguments.callee.caller.arguments[0];
		if (e && e.keyCode == 13)
			login();		
	}
});